<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Admin\Controller;

class AreaController extends BaseController {
    /*
     * 获取地区
     */

    public function index() {
		$mod = M('region');
		$region = $this->getCate('region');
		
		$this->assign('list', $region);
        $this->display();
    }
	
	public function add() {
		$mod = M('region');
		$mod_list = M('region_list');
		$data = I('post.');
		if ($data['act'] == 'add') {
			$mod = M('region');
			if (!$data['name']) {
                $this->error('请选择区域');
            }
			if($data['level']<3){
				$data['rid'] = $mod_list->where(array('name'=>$data['name']))->getField('id');
			}else{
				$data['rid'] = 0;
			}
			if($data['level']==4){
				$address1 = $mod->where(array('id'=>$data['pid']))->field('name,pid')->find();
				$address2 = $mod->where(array('id'=>$address1['pid']))->field('name,pid')->find();
				$address3 = $mod->where(array('id'=>$address2['pid']))->field('name,pid')->find();
				$address = $address3['name'].$address2['name'].$address1['name'].$data['name'];
				$a = json_decode(file_get_contents('http://api.map.baidu.com/geocoder/v2/?ak=vifyl3ep9FyosLXmwEgBPzEQ&output=json&address='.$address),true);
				$data['lng'] = $a['result']['location']['lng'];
				$data['lat'] = $a['result']['location']['lat'];
			}
			$data['up_time'] = $data['add_time'] = time();
			$level = $mod->where(array('id'=>$data['pid']))->getField('level');
			$data['level'] = $level?$level+1:1;
			$result = $mod->add($data);
			if($result){
				$this->success('添加成功',U('Admin/Area/index'));
			}else{
				$this->error('添加失败');
			}
		}else{
			$rid = $_GET['rid']?intval($_GET['rid']):'';
			$id = $_GET['id']?intval($_GET['id']):0;
			if($rid){
				$where = 'parent_id='.$rid;
			}else{
				$where = 'level=1';
			}
			$level = $mod->where(array('id'=>$id))->getField('level');
			$region = $mod_list->where($where)->field('name')->select();
			$this->assign('region', $region);
			$this->assign('id', $id);
			$this->assign('level', $level);
			$this->display();
		}
    }
	
	function edit(){
		$mod = M('region');
		$mod_list = M('region_list');
		$data = I('post.');
		if ($data['act'] == 'edit') {
			$data['up_time'] = time();
			if($data['level']==4){
				$address1 = $mod->where(array('id'=>$data['pid']))->field('name,pid')->find();
				$address2 = $mod->where(array('id'=>$address1['pid']))->field('name,pid')->find();
				$address3 = $mod->where(array('id'=>$address2['pid']))->field('name,pid')->find();
				$address = $address3['name'].$address2['name'].$address1['name'].$data['name'];
				$a = json_decode(file_get_contents('http://api.map.baidu.com/geocoder/v2/?ak=vifyl3ep9FyosLXmwEgBPzEQ&output=json&address='.$address),true);
				$data['lng'] = $a['result']['location']['lng'];
				$data['lat'] = $a['result']['location']['lat'];
			}
			$result = $mod->save($data);
			if($result){
				$this->success('编辑成功',U('Admin/Area/index'));
			}else{
				$this->error('编辑失败');
			}
		}else{
			$id = $_GET['id']?intval($_GET['id']):0;
			$region = $mod->where(array('id'=>$id))->field('level,name,pid')->find();
			$this->assign('name',$region['name']);
			$this->assign('id', $id);
			$rid = $mod->where(array('id='.$region['pid']))->getField('rid');
			if($rid){
				$region_list = $mod_list->where(array('parent_id='.$rid))->field('name')->select();
			}else{
				$region_list = $mod_list->where(array('level=1'))->field('name')->select();
			}
			$this->assign('region',$region_list);
			$this->assign('level',intval($region['level']));
			$this->assign('pid',$region['pid']);
			$this->display();
		}
	}
	
	function del(){
		$mod = M('region');
		$id = $_GET['id']?intval($_GET['id']):0;
		if($id){
			$result = $mod->where(array('id'=>$id))->delete();
			if($result){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}else{
			$this->error('请选择要删除的内容');
		}
	}
}
